/*********************************
Author: Accenture IDC OOR
Purpose: To add and update Assignment group members 
version 1.0
*********************************/
trigger OORSyncAssignmentGroupMember on User (after insert,after Update) {
    list<Assignment_Groups__c> Groupmembers= new list<Assignment_Groups__c>();
    list<Assignment_Groups__c> GroupmembersforDelete= new list<Assignment_Groups__c>();
    Map<String,Assignment_Group_Name__c> Groupname= new Map<String,Assignment_Group_Name__c>();
    List<User> UserNew = new List<User>();
    Set<String> OfficeCodes = new Set<String>();
    Set<Id> UserIdSet = new Set<Id>();
    Id mcProfileId;
    //mcProfileId = [select id from profile where name='SFA OOR Media Consultants' limit 1].Id;

  	IF(TRIGGER.ISINSERT){
   for(User user:Trigger.new){
      //if(user.profileId==mcProfileId){
       if(user.SFA_Office__c != NULL && user.OOR_Hire_Date__c != NULL){
           //UserIdSet.add(user.Id);
           UserNew.add(user);
           //OfficeCodes.add(user.SFA_Office__c);
       }
      //}
   } 
   If(UserNew.size()>0){
       mcProfileId = [select id from profile where name='SFA OOR Media Consultants' limit 1].Id;
         for(integer i=0;i<UserNew.size();i++){  
           if(UserNew[i].profileId!=mcProfileId){
               UserNew.remove(i);
               
           }
           else{
               UserIdSet.add(UserNew[i].Id);
               OfficeCodes.add(UserNew[i].SFA_Office__c);
           }
         }
   }
   IF(UserNew.size()>0){
       OORLeadRoundRobinQueue.ASmember_insert(UserIdSet);
   }
   }
   
   IF(TRIGGER.ISUPDATE){
      //mcProfileId = [select id from profile where name='SFA OOR Media Consultants' limit 1].Id;
      for(User user:Trigger.new){
         //if(user.profileId==mcProfileId){
          if(user.SFA_Office__c != NULL && user.OOR_Hire_Date__c != NULL && user.SFA_Office__c != Trigger.OldMap.get(user.Id).SFA_Office__c){
               //UserIdSet.add(user.Id);
               UserNew.add(user);  
               //OfficeCodes.add(user.SFA_Office__c); 
          }
       //} 
      }
      If(UserNew.size()>0){
       mcProfileId = [select id from profile where name='SFA OOR Media Consultants' limit 1].Id;
         for(integer i=0;i<UserNew.size();i++){  
           if(UserNew[i].profileId!=mcProfileId){
               UserNew.remove(i);
               
           }
           else{
               UserIdSet.add(UserNew[i].Id);
               OfficeCodes.add(UserNew[i].SFA_Office__c);
           }
         }
   }
      If(UserIdSet.size()>0){
          GroupmembersforDelete = [Select Id from Assignment_Groups__c where User__c IN:UserIdSet];
      }
      
      If(GroupmembersforDelete.size()>0){
          try{
          Delete GroupmembersforDelete;
          }
          catch(DMLException e){
              
          }
      }
      
   if(UserNew.size()>0){
     for(Assignment_Group_Name__c AssGroup:[select id,Name from Assignment_Group_Name__c where Type__c = 'Lead']){
           Groupname.put(AssGroup.Name,AssGroup);
       
       }
   }
   for(User user:UserNew){
       Assignment_Groups__c Groupmember= new Assignment_Groups__c();
       Groupmember.User__c = user.Id;
           for(String Str:Groupname.keySet()){
               if(Str.Contains(user.SFA_Office__c)){
                   Groupmember.Group_Name__c = Groupname.get(Str).Id;
                   
                   break;
               }
           }
       if(Groupmember.Group_Name__c != NULL){    
       Groupmembers.add(Groupmember);
       }
       
   }
   
   if(Groupmembers.size()>0){
       try{
           Insert Groupmembers;
       }
       Catch(DMLException e){
           GeneralConstants.mailAnyExceptionToMe('OORSyncAssignmentGroupMember',' Trigger for Adding Group Members',e);    
           System.debug('Exception while adding or Updatingf Assignment Group Members: ' + e); 
                 }
   }

      
   }
   
 }